import router from '@/router'
import store from '@/store'
import asyncRoute from '@/router/asyncRouter'

const whiteList = ['/login', '/404']

//获取一级权限的标识
function getFirstRoutePermission(permission) {
  const firstPermArr = permission.map(item => {
    return item.split(':')[0]
  })
  //去重，set不能存重复的数据（去重），但不是真数组，要转为真数组
  return Array.from(new Set(firstPermArr))
}

//获取二级权限标识
function getSecondRoutePermission(permission) {
  const secondPermArr = permission.map(item => {
    const arr = item.split(':')
    return `${arr[0]}:${arr[1]}`
  })
  return Array.from(new Set(secondPermArr))
}

//根据一级二级权限标识筛选出对应可展示的动态路由
function getRoutes(firstPermission, secondPermission, asyncRoute) {
  //判断当前登录人是不是管理员，如果是就不进行筛选了
  if (firstPermission.includes('*')) {
    return asyncRoute
  }
  const firstRoutes = asyncRoute.filter(item => firstPermission.includes(item.permission))
  const routes = firstRoutes.map(item => {
    return {
      ...item, 
      children: item.children.filter(child => secondPermission.includes(child.permission))
    }
  })
  return routes
}

router.beforeEach(async(to, from, next) => {
    //to 跳转的路由对象
    //from从哪里来
    //next放行，重定向
    const token = store.state.user.token
    if (token) {
      if (to.path === '/login') {
        next('/')
      } else {
        next()
        //1.拿到用户的权限信息，筛选
        const permission = await store.dispatch('menu/getUserPermission')
        //console.log('全部的权限标识', permission)
        //2.根据权限标识，筛选出对应的一级路由的标识
        const firstPermission = getFirstRoutePermission(permission)
        //console.log('一级权限标识', firstPermission)
        //3.根据权限标识，筛选出对应的二级路由的标识
        const secondPermission = getSecondRoutePermission(permission)
        //console.log('二级权限标识', secondPermission)
        //4.根据一级权限标识和二级权限标识和动态路由进行筛选
        // console.log(asyncRoute)
        const routes = getRoutes(firstPermission, secondPermission, asyncRoute)
        //console.log(routes)
        //5.把筛选后的路由展示在左侧
        //5.1先把筛选之后的路由添加到路由对象中（跳转）
        routes.forEach(item => router.addRoute(item))
        //5.2再把筛选之后的路由添加到vuex中（渲染）
        store.commit('menu/setMenuList', routes)

      }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
})